FaizanAnwerAli / Aptoide Apk Downloader JavaScript (Download APK directly to PC) [Beta] by (GL)BigBrother

// ==UserScript==
// @id              AptoideApkDownloaderScript
// @name            Aptoide Apk Downloader JavaScript (Download APK directly to PC) [Beta] by (GL)BigBrother
// @version         0.3
// @description     Download APK directly from Aptoide to PC
// @icon            http://i.imgur.com/Bacdt2p.png
// @icon64          http://i.imgur.com/et1aIiO.png
// @author          Faizan Anwer Ali <faizananwer2011@gmail.com>
// @match           http://*.aptoide.com/*
// @match           http://*.store.aptoide.com/*
// @match           https://*.aptoide.com/*
// @match           https://*.store.aptoide.com/*
// @include         http://*.aptoide.com/*
// @include         http://*.store.aptoide.com/*
// @include         https://*.aptoide.com/*
// @include         https://*.store.aptoide.com/*
// @exclude         https://*.aptoide.com/list/versions/*
// @exclude         http://*.aptoide.com/list/versions/*
// @grant           none
// @updateVersion   3
// ==/UserScript==


window.onload = function() {
    var srcccc = document.documentElement.innerHTML;
    var md5 = srcccc.match(/MD5:<\/strong> [A-Za-z0-9]*/).toString().slice(14);
    var number = "0";
    var errornumber = 0;
    var protocol = window.location.protocol;
    var url = window.location.href;
    if (((protocol === "http:") && (url.charAt(7) === "m") && (url.charAt(8) === ".")) || ((protocol === "https:") && (url.charAt(8) === "m") && (url.charAt(9) === "."))) {
        var css = '#Downloadmyapp{width: 155px; height: 33px; color: white; display: inline-block; position: absolute; top: 61px; float: right; font-size: 13px; font-family: Roboto, sans-serif; font-weight: bold; text-align: center; cursor: pointer; border-radius: 2px; border: 1px solid grey;}';
        var head = document.head || document.getElementsByTagName('head')[0];
        var style = document.createElement('style');
        style.type = 'text/css';
        style.id = 'Faizan';
        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }
        head.appendChild(style);

        var div2 = document.createElement("button");
        div2.id = "Downloadmyapp";
        div2.innerHTML = "Download XML File";
        var divnames = document.getElementsByClassName('app_view')[0].getElementsByTagName('div')[0].getElementsByClassName('app_meta_data')[0];
        divnames.insertBefore(div2, divnames.childNodes[0]);


        var div1 = document.createElement("button");
        div1.id = "DownloadApkToPC2";
        div1.style.width = "135px";
        div1.style.height = "33px";
        div1.style.color = "white";
        div1.style.display = "inline-block";
        div1.style.position = "absolute";
        div1.style.top = "61px";
        div1.style.cssFloat = "right";
        div1.style.fontSize = "13px";
        div1.style.fontFamily = "'Roboto',sans-serif";
        div1.style.fontWeight = "bold";
        div1.style.textAlign = "center";
        div1.style.cursor = "pointer";
        div1.style.borderRadius = "2px";
        div1.style.border = "1px solid grey";
        div1.innerHTML = "Download APK";
        if ((document.getElementsByClassName('btn app_install trusted')[0] != null) || (document.getElementsByClassName('btn app_install right trusted')[0] != null)) {
            div1.style.right = "267px";
            div2.style.right = "430px";
            div1.style.background = "#89d52d";
            div1.style.backgroundColor = "#89d52d";
            div2.style.backgroundColor = "#89d52d";
        } else if ((document.getElementsByClassName('btn app_install warning')[0] != null) || (document.getElementsByClassName('btn app_install right warn')[0] != null)) {
            div1.style.right = "270px";
            div2.style.right = "433px";
            div1.style.background = "#d6a52e";
            div1.style.backgroundColor = "#d6a52e";
            div2.style.backgroundColor = "#d6a52e";
        } else if ((document.getElementsByClassName('btn app_install unknown')[0] != null) || (document.getElementsByClassName('btn app_install right unknown')[0] != null)) {
            div1.style.right = "302px";
            div2.style.right = "465px";
            div1.style.background = "#808080";
            div1.style.backgroundColor = "#808080";
            div2.style.backgroundColor = "#808080";
        } else {
            div1.style.right = "300px";
            div2.style.right = "467px";
            div1.style.background = "#b3c833";
            div1.style.backgroundColor = "#b3c833";
            div2.style.backgroundColor = "#b3c833";
        }
        var divname1 = document.getElementsByClassName('app_view')[0].getElementsByTagName('div')[0].getElementsByClassName('app_meta_data')[0];
        divname1.insertBefore(div1, divname1.childNodes[0]);
        document.getElementById("DownloadApkToPC2").addEventListener("click", myfaizanfunction);
        document.getElementById("Downloadmyapp").addEventListener("click", DownloadmyappbuttonClick);
    } else {
        var css = '#Downloadmyapp{width:160px;height:36px;color:#fff;display:inline-block;font-size:13px;font-family:Roboto,sans-serif;font-weight:700;text-align:center;cursor:pointer;border-radius:2px;border:1px solid grey;left:75px;right:0;position:absolute;margin-left:auto;margin-right:auto;}';
        var head = document.head || document.getElementsByTagName('head')[0];
        var style = document.createElement('style');
        style.type = 'text/css';
        style.id = 'Faizan';
        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }
        head.appendChild(style);

        var divs = document.createElement("button");
        divs.id = "Downloadmyapp";
        divs.innerHTML = "Download XML File";
        var divnames = document.getElementsByClassName('app_view left')[0].getElementsByTagName('div')[0];
        divnames.insertBefore(divs, divnames.childNodes[0]);

        var div = document.createElement("button");
        div.id = "DownloadApkToPC";
        div.style.width = "135px";
        div.style.height = "36px";
        div.style.color = "white";
        div.style.display = "inline-block";
        div.style.fontSize = "13px";
        div.style.fontFamily = "'Roboto',sans-serif";
        div.style.fontWeight = "bold";
        div.style.textAlign = "center";
        div.style.cursor = "pointer";
        div.style.borderRadius = "2px";
        div.style.border = "1px solid grey";
        div.innerHTML = "Download APK";
        var trustedColor = "#89d52d";
        var modifiedcolor = "#d6a52e";
        var unknownColor = "#808080";
        if ((document.getElementsByClassName('btn app_install trusted')[0] != null) || (document.getElementsByClassName('btn app_install right trusted')[0] != null)) {
            var isChrome = !!window.chrome;
            var isFirefox = typeof InstallTrigger !== 'undefined';
            if ((isChrome === 'true') || (navigator.userAgent.indexOf("Chrome") != -1)) {
                div.style.left = "600px";
                div.style.top = "73px";
                div.style.position = "relative";
                div.style.backgroundColor = trustedColor;
                divs.style.backgroundColor = trustedColor;
                divs.style.top = "199px";
            } else if ((isFirefox === 'true') || (navigator.userAgent.indexOf("Firefox") != -1)) {
                div.style.left = "400px";
                div.style.right = "0px";
                div.style.top = "163px";
                divs.style.top = "163px";
                div.style.position = "absolute";
                div.style.backgroundColor = trustedColor;
                div.style.marginLeft = "auto";
                div.style.marginRight = "auto";
                divs.style.backgroundColor = trustedColor;
            } else {
                divs.style.top = "73px";
                div.style.left = "600px";
                div.style.top = "73px";
                div.style.position = "relative";
                div.style.backgroundColor = trustedColor;
                divs.style.backgroundColor = trustedColor;
            }
        } else if ((document.getElementsByClassName('btn app_install warning')[0] != null) || (document.getElementsByClassName('btn app_install right warn')[0] != null)) {
            var isChromes = !!window.chrome;
            var isFirefoxx = typeof InstallTrigger !== 'undefined';
            if ((isChromes === 'true') || (navigator.userAgent.indexOf("Chrome") != -1)) {
                div.style.left = "600px";
                div.style.top = "73px";
                div.style.position = "relative";
                div.style.backgroundColor = modifiedcolor;
                divs.style.backgroundColor = modifiedcolor;
                divs.style.top = "199px";
            } else if ((isFirefoxx === 'true') || (navigator.userAgent.indexOf("Firefox") != -1)) {
                div.style.right = "0px";
                div.style.left = "400px";
                div.style.top = "163px";
                divs.style.top = "163px";
                div.style.position = "absolute";
                div.style.backgroundColor = modifiedcolor;
                div.style.marginLeft = "auto";
                div.style.marginRight = "auto";
                divs.style.backgroundColor = modifiedcolor;
            } else {
                divs.style.top = "73px";
                div.style.left = "600px";
                div.style.top = "73px";
                div.style.position = "relative";
                div.style.backgroundColor = modifiedcolor;
                divs.style.backgroundColor = modifiedcolor;
            }
        } else if ((document.getElementsByClassName('btn app_install unknown')[0] != null) || (document.getElementsByClassName('btn app_install right unknown')[0] != null)) {
            var isChromess = !!window.chrome;
            var isFirefoxxx = typeof InstallTrigger !== 'undefined';
            if ((isChromess === 'true') || (navigator.userAgent.indexOf("Chrome") != -1)) {
                div.style.left = "600px";
                div.style.top = "73px";
                div.style.position = "relative";
                div.style.backgroundColor = unknownColor;
                divs.style.backgroundColor = unknownColor;
                divs.style.top = "199px";
            } else if ((isFirefoxxx === 'true') || (navigator.userAgent.indexOf("Firefox") != -1)) {
                div.style.right = "0px";
                div.style.left = "400px";
                div.style.top = "163px";
                divs.style.top = "163px";
                div.style.position = "absolute";
                div.style.backgroundColor = unknownColor;
                div.style.marginLeft = "auto";
                div.style.marginRight = "auto";
                divs.style.backgroundColor = unknownColor;
            } else {
                divs.style.top = "73px";
                div.style.left = "600px";
                div.style.top = "73px";
                div.style.position = "relative";
                div.style.backgroundColor = unknownColor;
                divs.style.backgroundColor = unknownColor;
            }
        } else {
            var isChromesss = !!window.chrome;
            var isFirefoxxxx = typeof InstallTrigger !== 'undefined';
            if ((isChromesss === 'true') || (navigator.userAgent.indexOf("Chrome") != -1)) {
                div.style.left = "600px";
                div.style.top = "73px";
                div.style.position = "relative";
                div.style.backgroundColor = "#b3c833";
                divs.style.backgroundColor = "#b3c833";
                divs.style.top = "199px";
            } else if ((isFirefoxxxx === 'true') || (navigator.userAgent.indexOf("Firefox") != -1)) {
                div.style.right = "0px";
                div.style.left = "400px";
                div.style.top = "163px";
                divs.style.top = "163px";
                div.style.position = "absolute";
                div.style.backgroundColor = "#b3c833";
                divs.style.backgroundColor = "#b3c833";
                div.style.marginLeft = "auto";
                div.style.marginRight = "auto";
            } else {
                divs.style.top = "73px";
                div.style.left = "600px";
                div.style.top = "73px";
                div.style.position = "relative";
                div.style.backgroundColor = "#b3c833";
                divs.style.backgroundColor = "#b3c833";
            }
        }
        var divname = document.getElementsByClassName('app_view left')[0].getElementsByTagName('div')[0];
        divname.insertBefore(div, divname.childNodes[0]);
        document.getElementById("DownloadApkToPC").addEventListener("click", myfaizanfunction);
        document.getElementById("Downloadmyapp").addEventListener("click", DownloadmyappbuttonClick);
    }

    //======================================================================================================================
    //Starts of functions or methods

    function DownloadmyappbuttonClick() {
        var starturl = "http://imgs.aptoide.com/apks/";
        var midurl = "/aptoide-f63c6f2461f65f32b6d144d6d2ff982e/";
        if (protocol === "http:") {
            if ((url.charAt(7) === "m") && (url.charAt(8) === ".")) {
                var nameurl = url.search("store.aptoide.com");
                nameurl = nameurl - 1;
                var finalname = url.slice(9, nameurl);
                var n = url.search("aptoide.com/app/market/");
                n = n + 23;
                var m = url.lastIndexOf("/");
                var resturl = url.slice(n, m);
                resturl = resturl.replace(/\./g, "-");
                resturl = resturl.replace(/\_/g, "-");
                resturl = resturl.replace(/\//g, "-");
                resturl = resturl.toLowerCase();
                for (var i = 0; i <= 3; i++) {
                    var finalurl = starturl + i + midurl + finalname + "/" + resturl + ".myapp";
                    var iframe = document.createElement("iframe");
                    iframe.src = finalurl;
                    iframe.style.width = "0px";
                    iframe.style.height = "0px";
                    iframe.style.position = "absolute";
                    var iddd = "download" + i;
                    iframe.id = iddd;
                    var divnames2 = document.getElementsByClassName('app_view')[0].getElementsByTagName('div')[0].getElementsByClassName('app_meta_data')[0];
                    divnames2.insertBefore(iframe, divnames2.childNodes[0]);
                }
            } else {
                var nameurl = url.search("store.aptoide.com");
                nameurl = nameurl - 1;
                var finalname = url.slice(7, nameurl);
                var n = url.search("aptoide.com/app/market/");
                n = n + 23;
                var m = url.lastIndexOf("/");
                var resturl = url.slice(n, m);
                resturl = resturl.replace(/\./g, "-");
                resturl = resturl.replace(/\_/g, "-");
                resturl = resturl.replace(/\//g, "-");
                resturl = resturl.toLowerCase();
                for (var i = 0; i <= 3; i++) {
                    var finalurl = starturl + i + midurl + finalname + "/" + resturl + ".myapp";
                    var iframe = document.createElement("iframe");
                    iframe.src = finalurl;
                    iframe.style.width = "0px";
                    iframe.style.height = "0px";
                    iframe.style.position = "absolute";
                    var iddd = "download" + i;
                    iframe.id = iddd;
                    var divnames = document.getElementsByClassName('app_view left')[0].getElementsByTagName('div')[0];
                    divname.insertBefore(iframe, divname.childNodes[0]);
                }
            }
        } else {
            if ((url.charAt(8) === "m") && (url.charAt(9) === ".")) {
                var nameurl = url.search("store.aptoide.com");
                nameurl = nameurl - 1;
                var finalname = url.slice(10, nameurl);
                var n = url.search("aptoide.com/app/market/");
                n = n + 23;
                var m = url.lastIndexOf("/");
                var resturl = url.slice(n, m);
                resturl = resturl.replace(/\./g, "-");
                resturl = resturl.replace(/\_/g, "-");
                resturl = resturl.replace(/\//g, "-");
                resturl = resturl.toLowerCase();
                for (var i = 0; i <= 3; i++) {
                    var finalurl = starturl + i + midurl + finalname + "/" + resturl + ".myapp";
                    var iframe = document.createElement("iframe");
                    iframe.src = finalurl;
                    iframe.style.width = "0px";
                    iframe.style.height = "0px";
                    iframe.style.position = "absolute";
                    var iddd = "download" + i;
                    iframe.id = iddd;
                    var divnames2 = document.getElementsByClassName('app_view')[0].getElementsByTagName('div')[0].getElementsByClassName('app_meta_data')[0];
                    divnames2.insertBefore(iframe, divnames2.childNodes[0]);
                }
            } else {
                var nameurl = url.search("store.aptoide.com");
                nameurl = nameurl - 1;
                var finalname = url.slice(8, nameurl);
                var n = url.search("aptoide.com/app/market/");
                n = n + 23;
                var m = url.lastIndexOf("/");
                var resturl = url.slice(n, m);
                resturl = resturl.replace(/\./g, "-");
                resturl = resturl.replace(/\_/g, "-");
                resturl = resturl.replace(/\//g, "-");
                resturl = resturl.toLowerCase();
                for (var i = 0; i <= 3; i++) {
                    var finalurl = starturl + i + midurl + finalname + "/" + resturl + ".myapp";
                    var iframe = document.createElement("iframe");
                    iframe.src = finalurl;
                    iframe.style.width = "0px";
                    iframe.style.height = "0px";
                    iframe.style.position = "absolute";
                    var iddd = "download" + i;
                    iframe.id = iddd;
                    var divnames = document.getElementsByClassName('app_view left')[0].getElementsByTagName('div')[0];
                    divname.insertBefore(iframe, divname.childNodes[0]);
                }
            }
        }
    }


    function myfaizanfunction() {
        var protocol = window.location.protocol;
        var url = window.location.href;
        if (protocol === "http:") {
            if ((url.charAt(7) === "m") && (url.charAt(8) === ".")) {
                var o = url.search("store.aptoide.com");
                o = o - 1;
                var name = url.slice(9, o);
                var n = url.search("aptoide.com/app/market/");
                n = n + 23;
                var m = url.lastIndexOf("/");
                var resturl = url.slice(n, m);
                resturl = resturl.replace(/\./g, "-");
                resturl = resturl.replace(/\_/g, "-");
                resturl = resturl.replace(/\//g, "-");
                resturl = resturl.toLowerCase();
                if (md5.length == 32) {
                    var myurl = "http://pool.apk.aptoide.com/" + name + "/" + resturl + "-" + md5 + ".apk";
                    var input = prompt("Either Copy this Download Link\n                    OR\nClick OK button to Download APK", myurl);
                    if (input != null) {
                        window.location = myurl;
                    }
                } else {
                    if (errornumber > 1) {
                        md5 = prompt("Copy and Paste MD5 checksum from App Info Screen");
                    } else {
                        errornumber++;
                        alert("You didn't click on App Info button first.\n\n         It's just Beneath the App name. like this [+ Info]\n\nClick on App Info button for once.\nThen close App Info Screen.\nIt will fetch data Automatically.\nNow you can click on Download APK button again");
                    }
                }
            } else {
                var oo = url.search("store.aptoide.com");
                oo = oo - 1;
                var namell = url.slice(7, oo);
                var nn = url.search("aptoide.com/app/market/");
                nn = nn + 23;
                var mm = url.lastIndexOf("/");
                var resturll = url.slice(nn, mm);
                resturll = resturll.replace(/\./g, "-");
                resturll = resturll.replace(/\_/g, "-");
                resturll = resturll.replace(/\//g, "-");
                resturll = resturll.toLowerCase();
                if (md5.length == 32) {
                    var myurll = "http://pool.apk.aptoide.com/" + namell + "/" + resturll + "-" + md5 + ".apk";
                    var inputl = prompt("Either Copy this Download Link\n                    OR\nClick OK button to Download APK", myurll);
                    if (inputl != null) {
                        window.location = myurll;
                    }
                } else {
                    if (errornumber > 1) {
                        md5 = prompt("Copy and Paste MD5 checksum from App Info Screen");
                    } else {
                        errornumber++;
                        alert("You didn't click on App Info button first.\n\n         It's just Beneath the App name. like this [+ Info]\n\nClick on App Info button for once.\nThen close App Info Screen.\nIt will fetch data Automatically.\nNow you can click on Download APK button again");
                    }
                }
            }
        } else {
            if ((url.charAt(8) === "m") && (url.charAt(9) === ".")) {
                var ooo = url.search("store.aptoide.com");
                ooo = ooo - 1;
                var namelll = url.slice(10, ooo);
                var nnn = url.search("aptoide.com/app/market/");
                nnn = nnn + 23;
                var mmm = url.lastIndexOf("/");
                var resturlll = url.slice(nnn, mmm);
                resturlll = resturlll.replace(/\./g, "-");
                resturlll = resturlll.replace(/\_/g, "-");
                resturlll = resturlll.replace(/\//g, "-");
                resturlll = resturlll.toLowerCase();
                if (md5.length == 32) {
                    var myurlll = "http://pool.apk.aptoide.com/" + namelll + "/" + resturlll + "-" + md5 + ".apk";
                    var inputll = prompt("Either Copy this Download Link\n                    OR\nClick OK button to Download APK", myurlll);
                    if (inputll != null) {
                        window.location = myurlll;
                    }
                } else {
                    if (errornumber > 1) {
                        md5 = prompt("Copy and Paste MD5 checksum from App Info Screen");
                    } else {
                        errornumber++;
                        alert("You didn't click on App Info button first.\n\n         It's just Beneath the App name. like this [+ Info]\n\nClick on App Info button for once.\nThen close App Info Screen.\nIt will fetch data Automatically.\nNow you can click on Download APK button again");
                    }
                }
            } else {
                var o = url.search("store.aptoide.com");
                o = o - 1;
                var name = url.slice(8, o);
                var n = url.search("aptoide.com/app/market/");
                n = n + 23;
                var m = url.lastIndexOf("/");
                var resturl = url.slice(n, m);
                resturl = resturl.replace(/\./g, "-");
                resturl = resturl.replace(/\_/g, "-");
                resturl = resturl.replace(/\//g, "-");
                resturl = resturl.toLowerCase();
                if (md5.length == 32) {
                    var myurl = "http://pool.apk.aptoide.com/" + name + "/" + resturl + "-" + md5 + ".apk";
                    var input = prompt("Either Copy this Download Link\n                    OR\nClick OK button to Download APK", myurl);
                    if (input != null) {
                        window.location = myurl;
                    }
                } else {
                    if (errornumber > 1) {
                        md5 = prompt("Copy and Paste MD5 checksum from App Info Screen");
                    } else {
                        errornumber++;
                        alert("You didn't click on App Info button first.\n\n         It's just Beneath the App name. like this [+ Info]\n\nClick on App Info button for once.\nThen close App Info Screen.\nIt will fetch data Automatically.\nNow you can click on Download APK button again");
                    }
                }
            }
        }
    }
};